<?php

namespace Common\Controller;

class WxAuthController extends BaseController
{
    private $userInfo;
    private $wxUserLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->wxUserLogic = A('Common/WxUser', 'Logic');

        $userInfoRes = $this->wxUserLogic->getUserInfo();

        if (static::$res->isErr($userInfoRes)) {
            if (IS_AJAX) {
                $this->ajaxReturn(static::$res->err("登陆信息异常"));
            } else {
                //走微信鉴权
                $parentId = I('get.parentId');
                redirect(C("WEB_DOMAIN")."/Wx/index?parentId=" . $parentId);
                exit;
            }
        }

        $userInfo = $userInfoRes['Result'];

        //账号被禁用的情况
        if ($userInfo['user_state'] == -2) {
            if (IS_AJAX) {
                $this->ajaxReturn(static::$res->err("您的账号已被禁用"));
            } else {
                $this->sysErr("您的账号已被禁用");
                exit;
            }
        }

        $this->setUserInfo($userInfo);
    }

    public function setUserInfo($userInfo)
    {
        $this->userInfo = $userInfo;
    }

    public function getUserInfo()
    {
        return $this->userInfo;
    }
}
